package Plugins;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
/**
 * This class implements method to load plugins
 * @author Guy & Liel
 *
 */
public class Loader 
{
	/**
	 * loading plugin
	 * @param filePath
	 * @return 	FileLabeler
	 */
	public FileLabeler loadFile(String filePath){
		FileLabeler fl=null;

		int lastLocation = filePath.lastIndexOf("\\");
		int lastPointLocation=filePath.lastIndexOf(".");
		//take the folder where the class file in
		String binPath = (String) filePath.subSequence(0,lastLocation);
		System.out.println("binPath:"+binPath);
		File file = new File(binPath);
		try
		{
			// Convert File to a URL
			URL url = file.toURI().toURL();
			URL[] urls = new URL[]{url};
			// Create a new class loader with the directory
			ClassLoader cl = new URLClassLoader(urls);
			String loadString = (String) filePath.subSequence(lastLocation+1, lastPointLocation);
			System.out.println(loadString);
			Class cls = cl.loadClass(loadString);
			fl=(FileLabeler) cls.newInstance();
		}

		catch (MalformedURLException e)	{} 
		catch (ClassNotFoundException e){} 
		catch (InstantiationException e){} 
		catch (IllegalAccessException e){}
		return fl;
	}
}


